package com.dotflat.staffExtend.controller;

import com.dotflat.rest.controller.RestfulBaseController;
import com.dotflat.staffExtend.domain.StaffExtendInfoRank;
import com.dotflat.staffExtend.domain.StaffExtendInfoRankCircle;
import com.dotflat.staffExtend.domain.StaffExtendInfoRankLog;
import com.dotflat.staffExtend.po.StaffExtendInfoRankLogPO;
import com.dotflat.staffExtend.repository.StaffExtendInfoRankCircleRepository;
import com.dotflat.staffExtend.service.StaffExtendInfoRankLogService;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.dto.StaffDTO;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.foundation.vo.StaffVO;
import com.starinside.wp.repository.StaffNativeRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/v2/staff_rank/log")
@Slf4j
@Api(tags = "员工评分 - 评分记录流水（通过新增流水来变更员工评分）")
public class APIStaffExtendInfoRankLogController extends RestfulBaseController<StaffExtendInfoRankLog, StaffExtendInfoRankLogService, StaffExtendInfoRankLogPO> {

    @Autowired
    private StaffExtendInfoRankCircleRepository circleRepository;

    @Autowired
    public void setBaseService(StaffExtendInfoRankLogService service){
        super.setBaseService(service);
    }

    @Autowired
    private StaffNativeRepository staffNativeRepository;

    @ApiOperation(value = "teachers")
    @GetMapping("/teachers")
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, HttpServletRequest req,
                                  String childId, PagerQuery pagerQuery){

        childId = validChildrenId(childId, req);

        List<StaffVO> staffs = staffNativeRepository.getStaffInClassRelByChildId(childId);

        return StatusResponse.success(staffs);
    }

    @ApiOperation(value = "增加一条记录")
    @PostMapping()
    public StatusResponse add(@RequestHeader(TICKET) String ticket,  HttpServletRequest req,
                              StaffExtendInfoRankLogPO param){


        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

        BindedBabyVO bindedBaby = loginUser.getDefaultBaby();
        StaffExtendInfoRankCircle circle = circleRepository.findFirstByAreaId(bindedBaby.getAreaId());

        if (circle == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "没有可用的评分周期");
        }

        param.setAreaId(bindedBaby.getAreaId());
        param.setSourceId(loginUser.getId());
        param.setSourceType(1);
        param.setSourceHeadImgUrl(loginUser.getHeadImgUrl());
        param.setRankCircleId(circle.getId());
        param.setSourceName(bindedBaby.getName() + bindedBaby.getRelation());

        StaffExtendInfoRankLog ret = baseService.add(bindedBaby.getAreaId(), loginUser.getId(), param);

        List<StaffExtendInfoRankLog> tmp = new ArrayList<>();
        tmp.add(ret);

        return StatusResponse.success(tmp);
    }
}
